package com.cndw.kungfu.config;

import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.cndw.kungfu.core.CacheDataBase;
import com.cndw.kungfu.core.CacheDataKey;
import com.cndw.kungfu.model.Context;

/**
 * 系统运行缓存对象
 * 
 * @author fantadust [9677889@qq.com]
 * @date 2011-10-6 下午2:13:02
 */
public class SystemCache extends CacheDataBase implements Serializable {

	public static final String unionTechLastTime = "unionTechLastTime";

	/**
	 * 门派资源上次刷新时间
	 */
	private int gestLastRefresh;
	/**
	 * 一些模块的时间记录
	 */
	private static Map<String, Integer> lastTimes = new ConcurrentHashMap<String, Integer>();

	private static SystemCache instance;
	
	private static final long serialVersionUID = -1029677889102L;

	private SystemCache() {

	}

	public static SystemCache getInstance() {
		if (instance == null) {
			synchronized (SystemCache.class) {
				if (instance == null) {
					CacheDataBase cacheData = Context.getSysServer().initCacheData(CacheDataKey.keyCacheData);
					if (cacheData != null && cacheData instanceof SystemCache) {
						instance = (SystemCache) cacheData;
					}
					if (instance == null) {
						instance = new SystemCache();
					}
					if (instance.getSyncDBTime() == 0) {
						instance.commitSync();
					}
				}
			}
		}
		return instance;
	}

	public int getGestLastRefresh() {
		return gestLastRefresh;
	}

	public void setGestLastRefresh(int gestLastRefresh) {
		this.gestLastRefresh = gestLastRefresh;
	}

	public void putLastTime(String key, Integer time) {
		lastTimes.put(key, time);
	}

	public Integer getLastTime(String key) {
		if (!lastTimes.containsKey(key)) {
			return 0;
		}
		return lastTimes.get(key);
	}


	@Override
	public String getKey() {
		return CacheDataKey.keyCacheData;
	}

}
